package cn.zcy.springboot_web.config;

import cn.zcy.springboot_web.entity.User;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.*;
import com.sun.tracing.dtrace.ModuleAttributes;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.boot.jackson.JsonObjectSerializer;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.io.IOException;

/**
 * JSON序列化
 */
@JsonComponent
public class UserJsonCustom {

/*    public static class Serializer extends JsonObjectSerializer<User> {
          //序列化
        @Override
        protected void serializeObject(User user, JsonGenerator jgen, SerializerProvider provider) throws IOException {
        jgen.writeObjectField("id",user.getId());
        jgen.writeObjectField("uname","hhh");
        }
    }*/

    public static class Deserializer extends JsonObjectDeserializer<User>{

        @Override
        protected User deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) throws IOException {
            User user=new User();
            user.setId(tree.findValue("id").asInt());
            return user;
        }
    }
}
